<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExporter;
use App\Handlers\RedisHandler;
use App\Http\Controllers\Controller;
use App\Models\MsgDevice;
use App\Models\RateType;
use App\Models\Task;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;

class MsgDeviceController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('机器管理');
            $content->description('系统每五分钟计算一次');

            $content->row(function ($row) {
                $row->column(4, new InfoBox('总设备数', '', 'aqua', action('\App\Admin\Controllers\MsgDeviceController@index', []) ,
                    MsgDevice::count()));
                $row->column(4, new InfoBox('在线设备', '', 'green', action('\App\Admin\Controllers\MsgDeviceController@index', ['status' => 1]),
                    MsgDevice::where('status',1)->count()));
                $row->column(4, new InfoBox('离线设备', '', 'red', action('\App\Admin\Controllers\MsgDeviceController@index', ['status' => 2]),
                    MsgDevice::where('status',2)->count()));
            });

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('机器管理');

            $content->body($this->form('edit')->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('机器管理');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(MsgDevice::class, function (Grid $grid) {
            if (!request()->get('_sort')) $grid->model()->orderByDesc('last_active_time');
            $grid->id('ID')->sortable();
            $grid->column('name', '机器名')->sortable();
            $grid->column('version', '版本')->sortable();
            $grid->column('success_amount', '今日成功数')->sortable();
            $grid->column('failed_amount', '今日失败数')->sortable();
            $grid->column('action_num_in_five_minutes', '最近五分钟完成数')->sortable();
            $grid->column('last_active_time', '最后活动时间')->sortable();
            $grid->column('status', '状态')->sortable()->display(function ($value) {
                $statusColor = config('system.task.status_color')[$value];
                return "<span class=\"label label-$statusColor\">" . config('system.app_device.status')[$value] . "</span>";
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name', '设备名');
                $filter->equal('version', '版本');
                $filter->equal('status', '状态')->select(config('system.app_device.status'));
                $filter->between('last_active_time', '最后活动时间')->datetime();
            });
            $grid->exporter(new ExcelExporter());
            $grid->tools(function ($tools) {
//                $tools->append(new SyncButton());

            });
//            $grid->actions(function ($actions) {
//                $actions->prepend('<a class="btn btn-xs btn-primary "
//                href="' . action('\App\Admin\Controllers\AppBuyLogController@index', ['device_name' => $this->row->name]) . '">
//                <i class="fa fa-clock-o"></i> 运行记录</a>');
//            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($mode = 'create')
    {
        return Admin::form(MsgDevice::class, function (Form $form) use ($mode) {
            $form->display('id', 'ID');
            $form->text('name', '机器名称');
            $form->text('version', '版本');
            $form->select('status', '状态')->options(config('system.app_device.status'))->default(2);

        });
    }

    public function sync()
    {
        RedisHandler::syncMsgDevices();
        return ['status_code' => 200, 'message' => '同步成功'];
    }
}
